﻿/*!

 @Name：layer mobile v1.6 弹层组件移动版
 @Author：贤心
 @Date：2015-07-08
 @Copyright：Sentsin Xu(贤心)
 @官网：http://layer.layui.com/mobile/
 @License：MIT

 */

;
!function (win) {
    "use strict";
    var doc = document, query = 'querySelectorAll', claname = 'getElementsByClassName', S = function (s) {
        return doc[query](s);
    };
//默认配置
    var config = {
        type: 0,
        shade: true,
        shadeClose: true,
        fixed: true,
        anim: true,
        animClass: "layermanim"
    };

    win.ready = {
        extend: function (obj) {
            var newobj = JSON.parse(JSON.stringify(config));
            for (var i in obj) {
                newobj[i] = obj[i];
            }
            return newobj;
        },
        timer: {},
        end: {}
    };

//点触事件
    ready.touch = function (elem, fn) {
        var move;
        elem.addEventListener('touchmove', function () {
            move = true;
        }, false);
        elem.addEventListener('touchend', function (e) {
            e.preventDefault();
            move || fn.call(this, e);
            move = false;
        }, false);
    };

    var index = 0, classs = ['layermbox'], Layer = function (options) {
        var that = this;
        that.config = ready.extend(options);
        that.view();
    };

    Layer.prototype.view = function () {
        var that = this, config = that.config, layerbox = doc.createElement('div');

        that.id = layerbox.id = classs[0] + index;
        layerbox.setAttribute('class', classs[0] + ' ' + classs[0] + (config.type || 0));
        layerbox.setAttribute('index', index);
        layerbox.setAttribute('style','z-index:'+config.z);
        var title = (function () {
            var titype = typeof config.title === 'object';
            return config.title
                ? '<h3 style="' + (titype ? config.title[1] : '') + '">' + (titype ? config.title[0] : config.title) + '</h3><button class="layermend"></button>'
                : '';
        }());

        var button = (function () {
            var btns = (config.btn || []).length, btndom;
            if (btns === 0 || !config.btn) {
                return '';
            }
            btndom = '<span type="1">' + config.btn[0] + '</span>'
            if (btns === 2) {
                btndom = '<span type="0">' + config.btn[1] + '</span>' + btndom;
            }
            return '<div class="layermbtn">' + btndom + '</div>';
        }());

        if (!config.fixed) {
            config.top = config.hasOwnProperty('top') ? config.top : 100;
            config.style = config.style || '';
            config.style += ' top:' + ( doc.body.scrollTop + config.top) + 'px';
        }

        if (config.type === 2) {
            config.content = '<i></i><i class="laymloadtwo"></i><i></i><div>' + (config.content || '') + '</div>';
        }

        layerbox.innerHTML = (config.shade ? '<div ' + (typeof config.shade === 'string' ? 'style="' + config.shade + '"' : '') + ' class="laymshade"></div>' : '')
            + '<div class="layermmain" ' + (!config.fixed ? 'style="position:static;"' : '') + '>'
            + '<div class="section">'
            + '<div class="layermchild ' + (config.className ? config.className : '') + ' ' + ((!config.type && !config.shade) ? 'layermborder ' : '') + (config.anim ? config.animClass : '') + '" ' + ( config.style ? 'style="' + config.style + '"' : '' ) + '>'
            + title
            + '<div class="layermcont">' + config.content + '</div>'
            + button
            + '</div>'
            + '</div>'
            + '</div>';

        if (!config.type || config.type === 2) {
            var dialogs = doc[claname](classs[0] + config.type), dialen = dialogs.length;
            if (dialen >= 1) {
                layer.close(dialogs[0].getAttribute('index'))
            }
        }
        $("body").append(layerbox);
        //document.body.appendChild(layerbox);
        var elem = that.elem = S('#' + that.id)[0];
        config.success && config.success(elem);

        that.index = index++;
        that.action(config, elem);
    };

    Layer.prototype.action = function (config, elem) {
        var that = this;

        //自动关闭
        if (config.time) {
            ready.timer[that.index] = setTimeout(function () {
                layer.close(that.index);
            }, config.time * 1000);
        }

        //关闭按钮
        if (config.title) {
            var end = elem[claname]('layermend')[0], endfn = function () {
                config.cancel && config.cancel();
                layer.close(that.index);
            };
            ready.touch(end, endfn);
            end.onclick = endfn;
        }

        //确认取消
        var btn = function () {
            var type = this.getAttribute('type');
            if (type == 0) {
                config.no && config.no();
                layer.close(that.index);
            } else {
                config.yes ? config.yes(that.index) : layer.close(that.index);
            }
        };
        if (config.btn) {
            var btns = elem[claname]('layermbtn')[0].children, btnlen = btns.length;
            for (var ii = 0; ii < btnlen; ii++) {
                ready.touch(btns[ii], btn);
                btns[ii].onclick = btn;
            }
        }

        //点遮罩关闭
        if (config.shade && config.shadeClose) {
            var shade = elem[claname]('laymshade')[0];
            ready.touch(shade, function () {
                config.shadeClick ? config.shadeClick(that.index) :
                layer.close(that.index, config.end);
            });
            shade.onclick = function () {
                config.shadeClick ? config.shadeClick(that.index) :
                layer.close(that.index, config.end);
            };
        }

        config.end && (ready.end[that.index] = config.end);
    };

    var layer = {
        v: '1.6',
        index: index,

        //核心方法
        open: function (options) {
            var o = new Layer(options || {});
            return o.index;
        },

        close: function (index) {
            var ibox = S('#' + classs[0] + index)[0];
            if (!ibox) return;
            ibox.innerHTML = '';
            doc.body.removeChild(ibox);
            clearTimeout(ready.timer[index]);
            delete ready.timer[index];
            typeof ready.end[index] === 'function' && ready.end[index]();
            delete ready.end[index];
        },

        //关闭所有layer层
        closeAll: function () {
            var boxs = doc[claname](classs[0]);
            for (var i = 0, len = boxs.length; i < len; i++) {
                layer.close((boxs[0].getAttribute('index') | 0));
            }
        },
        closeById:function(id){
            layer.close($("#"+id).attr("data-index"));
        },
        closes:function(elem){
            var index=$($(elem).parents(".layermbox")[0]).attr("index");
            layer.close(index);
        },
        //自定义的
        wait: function (message) {
            var index=layer.open({
                type: 2,
                content: message
                // shadeClose:false
            });
            return index;
        },
        //不关闭的
        waiting: function (message) {
            var index=layer.open({
                type: 2,
                content: message,
                shadeClose: false
            });
            return index;
        },
        deng: function (message,id) {
            var index=layer.open({
                type: 2,
                content: message,
                shadeClose: false,
                success:function(elem){
                    var index=$(elem).attr("index");
                    $(elem).find(".layermmain").attr("id",id).attr("data-index",index);
                }
            });
            return index;
        },
        error: function (message) {
            var index=layer.open({
                anim: false,
                content: message,
                btn: ['确认'],
                z:99999999,
                style: "text-align:center;"

            });
            return index;
           /* var index=layer.open({
                type: 1,
                content: message,
                animClass: 'fadeInDown',
                style: 'position:absolute;left:0;top:0px;width:100%;border:none;font-size: 16px;padding: 13px;' +
                    'background-color: #de4343;border-color: #c43d3d;text-align: center;color: #fff;'
            });
            return index;*/
        },
        info: function (message) {
            var index=layer.open({
                anim: false,
                content: message,
                shade: false,
                style: " padding:0;width:0;border:none;",
                z:99999999,
                time: 1.6
            });
            return index;
      /*      var index=layer.open({
                type: 1,
                content: message,
                time: 2,
                animClass: 'fadeInDown',
                shade: false,
                z:99999999,
                style: 'position:absolute;left:0;top:0px;width:100%;border:none;font-size: 16px;padding: 13px;' +
                    'background-color: #72c246;border-color: #72c246;text-align: center;color: #fff;'
            });
            return index;*/
        },
        upload: function (message,jqXHR) {
            var html=message+'<a href="javacript:void(0)" class="abort">取消上传</a><div id="progress"><div class="bar" style="width: 0%;height: 18px;background: green;"></div></div>';
            var index=layer.open({
                type: 1,
                content: html,
                animClass: 'fadeInDown',
                shadeClose: false,
                style: 'position:absolute;left:0;top:0px;width:100%;border:none;font-size: 16px;padding: 13px;' +
                    'background-color: #72c246;border-color: #72c246;text-align: center;color: #fff;',
                success:function(elem){
                    $(elem).find(".abort").click(function(){
                        jqXHR.abort();
                    })
                }

            });
            return index;
        },
        down: function (html, success) {
            var index=layer.open({
                type: 1,
                content: html,
                animClass: 'fadeInDown',
                style: 'position:absolute;left:0;top:0px;width:100%;padding:0px 0 10px;border:none; max-height:80%;overflow:auto;overflow-x: hidden',
                success: success
            });
            return index;
        },
        up: function (html, success) {
            var index=layer.open({
                type: 1,
                content: html,
                animClass: 'fadeInUp',
                style: 'position:absolute;left:0;bottom:0;width:100%; padding:10px 0 20px;border:none;max-height:80%;overflow:auto;overflow-x: hidden',
                success: success
            });
            return index;
        },
        center: function (html, success,shadeClick) {
            var index=layer.open({
                type: 1,
                content: html,
                animClass:'',
                style: 'border-radius: 5px; border:none;',
                success: success,
                shadeClick:shadeClick
            });
            return index;
        },
        centerNoFix: function (html, success,shadeClick,style) {
            if(typeof style=="undefined"){
                style="";
            }
            var index=layer.open({
                type: 1,
                content: html,
                fixed:false,
                animClass:'',
                style: 'border-radius: 5px; border:none;'+style,
                success: success,
                shadeClick:shadeClick
            });
            return index;
        },
        notify: function (text) {
            var index=layer.open({
                content: text,
                style: "text-align:center;",
                time: 1.3
            });
            return index;
        },
        alertInfo: function (text) {
            var index=layer.open({
                anim: false,
                content: text,
                shade: false,
                style: " padding:0;width:0;border:none;",
                time: 1.6
            });
            return index;
        },
        alert: function (text) {
            var index=layer.open({
                anim: false,
                content: text,
                btn: ['确认'],
                style: "text-align:center;"
            });
            return index;
        },
        confirm: function (text, yesCallback, noCallback) {
            var index=layer.open({
                anim: false,
                content: text,
                btn: ['取消', '确定'],
                shadeClose: false,
                yes: function (index) {
                    if (typeof noCallback != 'undefined') {
                        noCallback();
                    }
                    layer.close(index);
                }, no: function () {
                    if (typeof yesCallback != 'undefined') {
                        yesCallback();
                    }

                }
            });
            return index;
        }

    };
    'function' === typeof define ? define(function () {
        return layer;
    }) : win.layer = layer;

}(window);
if(typeof  _msgx!="undefined"){
    if(_msgx.error!=''){
        layer.error(_msgx.error);
    }
    if(_msgx.info!=''){
        layer.info(_msgx.info);
    }
}